﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DynaPrecision.FastFourierTransform
{
	public class ChebyshevRealTransform
	{
		public static void Transform<T>(IList<T> from, int Sign = 1)
		{
			if (Sign != -1 && Sign != 1)
			{
				throw new System.Exception("Only 1 and -1 are allowed for Sign paramter in Chebyshev Transform");
			}
			if (Sign == -1)
			{
				from[0] = (dynamic)from[0] * 2;
				from[from.Count - 1] = (dynamic)from[from.Count - 1] * 2;
			}
			//Obtain the order of the result
			int order = from.Count - 1;
			if ((1 << StandardComplexTransform.OrderOf2(order)) != order)
			{
				throw new Exception("Power of 2 required for FFT");
			}
			//Resort the result into a new array to FFT
			List<Complex<T>> vj = new List<Complex<T>>();
			for (int i = 0; i < order + 1; i++)
			{
				vj.Add(new Complex<T> { Real = from[i] });
			}
			for (int i = order + 1; i < 2 * order; i++)
			{
				vj.Add(new Complex<T> { Real = from[2 * order - i] });
			}
			StandardComplexTransform.Transform(vj);
			for (int i = 0; i < order + 1; i++)
			{
				from[i] = vj[i].Real / (dynamic)2;
			}
		}

		public static void TransformTo<T>(IList<T> from, IList<T> to, int Sign = 1)
		{
			int i = 0;
			for (; i < from.Count && i < to.Count; i++)
			{
				to[i] = from[i];
			}
			for (; i < from.Count; i++)
			{
				to.Add(from[i]);
			}
			Transform(to, Sign);
		}
	}
}
